<?php
	include("../connect/connect.php");
	// เพิ่มข้อมูลใน event
	$sql = "select * from rev_vmsmentalhelp where sendto_vms <> 1";
	$rs=mysql_query($sql);
	//echo "<br>".$sql;
	while($row = mysql_fetch_array($rs)){//
		//get pk  --> evnid
		$victim_idcardno=$row["victim_idcardno"];
		$sql = "select victim_idcardno from vmsmentalhelp where victim_idcardno='$victim_idcardno'";
		//echo "<br>".$sql;
		$rs2=mysql_query($sql);

		$eval_idcardno=$row["eval_idcardno"];
		$visitno=$row["visitno"];
		$visitdate=$row["visitdate"];
		$placevisit=$row["placevisit"];
		$evaluare=$row["evaluare"];
		$mentalprob=$row["mentalprob"];
		$helpsubsidize=$row["helpsubsidize"];
		$surveyorname=$row["surveyorname"];
		
		//echo "<br>rs2=".$rs2;
		if($row2=mysql_fetch_array($rs2)){//update
			$sql="update vmsmentalhelp set eval_idcardno='$eval_idcardno', visitno='$visitno', visitdate='$visitdate', placevisit='$placevisit', evaluare='$evaluare', mentalprob='$mentalprob', helpsubsidize='$helpsubsidize',surveyorname='$surveyorname' where victim_idcardno='$victim_idcardno'";
		}else{//insert
			$sql="insert into vmsmentalhelp(victim_idcardno, eval_idcardno, visitno, visitdate, placevisit, evaluare, mentalprob, helpsubsidize,surveyorname) values('$victim_idcardno','$eval_idcardno','$visitno','$visitdate','$placevisit','$evaluare','$mentalprob','$helpsubsidize','$surveyorname')";
		}
		//echo "<br>".$sql;
		$rs2=mysql_query($sql);
		
	}//จบการวนลูปเพิ่มหรือปรับปรุง opmhelp
	
	//ปรับปรุง rev_opmhelp สถานะส่งแล้ว	
	$tempY=date("Y");
	if($tempY<"2550"){
		$tempY=(int)$tempY+543;
	}
	$datetime_send= $tempY.date("-m-d-H:i");
	
	$sql = "update rev_vmsmentalhelp set sendto_vms=1,datetime_sendto_vms='$datetime_send'  where sendto_vms=0;";
	$rs=mysql_query($sql);
	mysql_close($c);
	echo "<br><br>complete insert/update vms mental help";
?>